//
//  ProductDetail_Vm.swift
//  Pods
//
//  Created by eme on 2017/3/28.
//
//

import RxSwift
import NetWorkCore
import SwiftyJSON
import UtilCore
/*
 owner:cy
 update:2016年12月17日12:02:55
 info: 首页数据
 VC:   ProductDetail_Vc
 */
class AddCar_Vm :Base_Vm {
    
    /// 表示正在刷新中
    let loading = ActivityIndicator()
    let addcartsSuccess = PublishSubject<JSON>()
    let addcartsSubject = PublishSubject<NSString>()
    let createOrderSubject = PublishSubject<[String: AnyObject]>()
    let createOrderSuccess = PublishSubject<JSON>()
    /*
     通用设置
     */
    init(refreshTriger: Observable<String>) {
        super.init()
        addcartsSubject
            .map{ osCarts in return AddCarApi.addcarts(osCarts: osCarts)}
            .emeRequestApiForRegJson(loading)
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let data):
                    self.addcartsSuccess.onNext(data)
                case .failure(let error):
                    self.error.onNext(error)
                }
            }).disposed(by: disposeBag)
        
        createOrderSubject
            .map{ order in return AddCarApi.createOrder(order)}
            .emeRequestApiForRegJson(loading)
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let rel):
                    self.createOrderSuccess.onNext(rel)
                case .failure(let error):
                    self.error.onNext(error)
                }
            }).disposed(by: disposeBag)
        
        
        
    }
    
}
